package com.ggy.tuibi.Schedu;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.ggy.tuibi.entity.yuanqimoguicheng.YuanQiMoGuiChengUser;
import com.ggy.tuibi.service.yuanqimoguicheng.YuanQiMoGuiChengService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.spi.DateFormatProvider;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@EnableScheduling
@Component
@Slf4j
public class YuanQiMoGuiChengSchedu {
    @Autowired
    private YuanQiMoGuiChengService yuanQiMoGuiChengService;

    static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    static List<LocalDateTime> randomDateList = new ArrayList<>();

//    static {
//        initRandomDate();
//    }
//
//    public static void initRandomDate() {
//        randomDateList.clear();
//        for (int i = 0; i < 10; i++) {
//            LocalDateTime randomTime = getLocalDateTime();
//            randomDateList.add(randomTime);
//        }
//    }

    @Scheduled(cron = "0 0 4 * * ?")//每秒钟执行一次，以空格分隔
    public void signNo() {
        log.info("开始签到元气魔鬼城");
        yuanQiMoGuiChengService.yuanQiQianDao();
    }

    @Scheduled(cron = "0 * * * * ?")
    public void enroll() {
//        // Get the current LocalDateTime
//        LocalDateTime now = LocalDateTime.now();
//        // Truncate the LocalDateTime to the nearest minute
//        LocalDateTime truncatedLocalDateTime = now.truncatedTo(ChronoUnit.MINUTES);
//        log.info("当前时间:{}", truncatedLocalDateTime);
//        boolean isExecute = false;
//        for (LocalDateTime randomTime : randomDateList) {
//            log.info("随机时间list:{}", randomTime);
//            if (truncatedLocalDateTime.isEqual(randomTime)) {
//                isExecute = true;
//            }
//        }
//        if (isExecute) {
//            log.info("时间对上了");
//            yuanQiMoGuiChengService.enroll();
//        }
    }


    @Scheduled(cron = "0 1 0 * * ?")
    public void initData() {
//        List<YuanQiMoGuiChengUser> userAll = yuanQiMoGuiChengService.list();
//        List<YuanQiMoGuiChengUser> updateUserAll = new ArrayList<>();
//        for (YuanQiMoGuiChengUser user : userAll) {
//            user.setIs_sign_in("0");
//            user.setIs_shimobun("0");
//            updateUserAll.add(user);
//        }
//        yuanQiMoGuiChengService.updateBatchById(updateUserAll);
//
//        initRandomDate();

    }


    public static LocalDateTime getLocalDateTime() {
        //当前时间
        LocalDate nowLocalDate = LocalDate.now();
        StringBuffer nowLocalDateBuf = new StringBuffer(nowLocalDate.toString()).append(" ");
        Random random = new Random();
        int randomHours = random.nextInt(24);
        int randomMinutes = random.nextInt(60);
        //拼接小时
        nowLocalDateBuf.append(randomHours < 10 ? "0" + randomHours : randomHours).append(":");
        //拼接分钟
        nowLocalDateBuf.append(randomMinutes < 10 ? "0" + randomMinutes : randomMinutes).append(":00");
        LocalDateTime randomLocalDateTime = LocalDateTime.parse(nowLocalDateBuf, formatter);
        log.info("生成的随机时间:{}", randomLocalDateTime);
        return randomLocalDateTime;
    }
    public static void main(String[] args) {
        YuanQiMoGuiChengSchedu service = new YuanQiMoGuiChengSchedu();
        ExecutorService executorService = Executors.newCachedThreadPool();
        List<Runnable> taskList = new ArrayList<>();
        for (int i = 0; i < 500; i++) {
            taskList.add(() -> service.xiaji());
        }
        for (Runnable runnable : taskList) {
            executorService.submit(runnable);
        }

        executorService.shutdown();
    }

    private void xiaji() {
        Map<String, Object> bodyMap = new HashMap<>();
        bodyMap.put("machine_position", "4");
        bodyMap.put("machine_types", "10");
        bodyMap.put("user_code", "1172040");
        bodyMap.put("user_id", "tqdgRUb_3hcUCUpRI70jkRuhKCB7ohYh");
        bodyMap.put("machine_alias", "DpmkHR7Yrsir2z8gu00mMA");
        bodyMap.put("machine_id", "20230609042621493");
        bodyMap.put("IMEI", "A0481004-BA65-48C1-8328-7C6CCA3AEC1F");
        bodyMap.put("message", "9");
        HttpResponse response = HttpUtil.createPost("http://www.douyuewang.com/YuanQiJieJi_Web/doll_WanShengYe.do?mapStr=" + JSONUtil.toJsonStr(bodyMap)).execute();
        System.out.println(response.body());
    }

}
